<cfif Session.id EQ 0><cflocation url="../index.cfm"></cfif>

<cfobject name="comEntries" component="entries">
<cfparam name="action" default="">
<cfparam name="start" default="0">
<cfset filename = ListLast(GetCurrentTemplatePath(), "\")>
<cfswitch expression="#action#">
	<!--- Add --->
	<cfcase value="add">
		<cfinvoke
			component="#comEntries#"
			method="add"
			text="#text#"
			date="#date#" >
	</cfcase>
	<cfcase value="setSchool">
		<cfinvoke
			component="#comEntries#"
			method="setSchool"
			id="#id#"
			value="#value#" >
	</cfcase>
	
	<cfcase value="update">
		<cfscript>
			comEntries.edit(url.id, url.text);
		</cfscript>
	</cfcase>

	<cfcase value="addtmp">
		<cfscript>
			comEntries.addtmp(url.text);
		</cfscript>
	</cfcase>
	<cfcase value="deletetmp">
		<cfscript>
			comEntries.deletetmp(url.text);
		</cfscript>
	</cfcase>

	<cfcase value="move">
		<cfinvoke
			component="#comEntries#"
			method="#url.action#"
			id='#url.id#'
			position='#url.position#'>
	</cfcase>
	
	<!--- Delete --->
	<cfcase value="delete">
		<cfscript>
			comEntries.delete(id);
		</cfscript>
		Entry deleted.
	</cfcase>
	<cfcase value="countDays">
		<cfinvoke
			component="#comEntries#"
			method="countDays"
			returnVariable="max">
		{"amount":"<cfoutput>#max#</cfoutput>"}
	</cfcase>

	<cfcase value="listtmp">
<cfsavecontent variable="content">
		<cfinvoke
			component="#comEntries#"
			method="#url.action#"
			returnVariable="query">

<cfset i = 0>[ <cfoutput query="query">
		<cfif i NEQ 0> , </cfif>
		'#query.text#'
		<cfset i = i+1>
		</cfoutput> ]
</cfsavecontent><cfoutput>#Trim(content)#</cfoutput>
	</cfcase>

	<!--- Default --->
	<cfcase value="getEntriesList">
<cfsavecontent variable="content">
		<cfinvoke
			component="#comEntries#"
			method="#url.action#"
			start="#start#"
			returnVariable="query">

<cfset lastDate = ""><cfset i = 0>[ <cfoutput query="query">
			<cfif query.date neq lastDate>
<cfif i NEQ 0 >]},</cfif> { "date" : "#DateFormat(query.date, 'dd. ')&MonthAsString(DateFormat(query.date, 'm'))&DateFormat(query.date, ' yyyy')#", "id" : "#query.day_id#",  "school" : "#query.school#", "data" :	[
			<cfset i = i+1>
			<cfset j = 0>
			<cfset lastDate = query.date>
			</cfif>
<cfif j NEQ 0 >,</cfif>{ "text" : "#Replace(query.text, '"', '\"', 'all')#", "id" : "#query.id#", "sort_id" : "#query.sort_id#" }
			<cfset j = j+1>
			</cfoutput>
<cfif i NEQ 0 >]}</cfif>]
</cfsavecontent><cfoutput>#Trim(content)#</cfoutput>
	</cfcase>

	<cfdefaultcase>
		<cf_template i="1">
			<cfinvoke
				component="#comEntries#"
				method="countDays"
				returnVariable="max">
			<input type="hidden" value="<cfoutput>#max#</cfoutput>" id="info_max"> 
		</cf_template>
	</cfdefaultcase>
</cfswitch>